﻿namespace Cid
{
	using System;
	using System.Collections.Generic;
	using System.Configuration;
	using System.Data;
	using System.Linq;
	using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Media.Animation;
    using System.Windows.Media;

	using Cid.Data;
	using Cid.Models;

	/// <summary>
	/// Interaction logic for App.xaml
	/// </summary>
	public partial class App : Application
	{
		private string connectionString;

		protected override void OnStartup(StartupEventArgs e)
		{
			base.OnStartup(e);



#if DEBUG
			this.connectionString = ConfigurationManager.ConnectionStrings["development"].ConnectionString;
			ContextProxy.CreateNewContext = () => new CidDataContext(this.connectionString);
			//Fixtures runner =	Fixtures.New(
			//	System.IO.Path.GetDirectoryName(this.GetType().Assembly.Location) + "\\Fixtures",
			//	new Adapter(conn));
			//new string[] {"ConferenceEvents"}.Each(fixture => runner[fixture].RenewFixtures());
#else 
			this.connectionString = ConfigurationManager.ConnectionStrings["development"].ConnectionString;
			ContextProxy.CreateNewContext = () => new CidDataContext(this.connectionString);
#endif

		}

		protected override void OnExit(ExitEventArgs e)
		{

			try
			{
				new Adapter(this.connectionString).ExecuteNonQuery(
					"USE MASTER \n ALTER DATABASE [{0}] \n SET SINGLE_USER; exec sp_detach_db '{0}', 'true';".Fuse("cid_development"), false
				);
			}
			catch (System.Exception ex)
			{
				Console.WriteLine(ex);
			}

			

			base.OnExit(e);
		}

		protected override void OnNavigating(System.Windows.Navigation.NavigatingCancelEventArgs e)
		{
			base.OnNavigating(e);
		}

        private void Name_Loaded(object sender, RoutedEventArgs e)
        {
            AnimateText((TextBlock)sender);
        }

        private void AnimateText(TextBlock textBlock)
        {
            Storyboard storyboard = new Storyboard();
            storyboard.BeginTime = TimeSpan.FromSeconds(1);

            if (textBlock.TextEffects != null)
                textBlock.TextEffects.Clear();
            else
                textBlock.TextEffects = new TextEffectCollection();

            for (int i = 0; i < textBlock.Text.Length; i++)
            {
                // so define the effect
                TextEffect effect = new TextEffect();
                ScaleTransform scaleTransform = new ScaleTransform();
                scaleTransform.CenterY = textBlock.ActualHeight / 2;
                scaleTransform.ScaleY = 0;
                effect.PositionStart = i;
                effect.PositionCount = 1;

                effect.Transform = scaleTransform;

                textBlock.TextEffects.Add(effect);

                // This animation sets the value from 0 to 20 of SOMETHING. All it does is 
                // do the automatic timelining of setting that value for the given duration
                // and we apply that value setter to a particular property later (using the
                // property path object)
                DoubleAnimation animation = new DoubleAnimation(0, 1.0, TimeSpan.FromSeconds(.15));
                animation.AccelerationRatio = 0.5;
                animation.DecelerationRatio = 0.5;
                //animation.AutoReverse = true;
                //animation.RepeatBehavior = RepeatBehavior.Forever;
                animation.BeginTime = TimeSpan.FromSeconds(i * 0.03);

                // which object to apply the storyboard animation to
                Storyboard.SetTargetName(animation, textBlock.Name);
                // which property to apply the storyboard animation to
                Storyboard.SetTargetProperty(animation, new PropertyPath(String.Format("TextEffects[{0}].Transform.ScaleY", i)));

                //Storyboard.SetTargetProperty(animation2, new PropertyPath(String.Format("TextEffects[{0}].Transform.ScaleY", i)));
                storyboard.Children.Add(animation);
            }
            storyboard.Begin(textBlock);
        }
	}
}
